(function($){
	$.fn.tjTable = function(options){
		var _tablethis = this;
		var _default = {
			bSort : false,
			searching : false,
			bLengthChange:false,//默认不显示分页条数
			aLengthMenu : [ [ 15, 30, 50, 100 ],[ 15, 30, 50, 100 ] ],
			processing : true,
			serverSide : true,
			width : "auto",
			select:{type :'default',name:''},
			ajax :function(){},
			columns :[],
			click : function(){},
			dBclick : function(){}
		};
		var _settings = $.extend(_default, options);	
		
		/**
		 * 扩展表格复选和单选
		 */
		if(_settings.select.type=='radio'){
			var _radio = {
				"sTitle" : "",
				"sWidth" : "13",
				"render" : function(data, type, row) {
					return '<input type="radio" name="id[]" value="'
							+ row[_settings.select.name] + '">';
				}
			}				
			if(_settings.columns){
				var _newColumns = [];
				_newColumns.push(_radio);
				for(var i=0;i<_settings.columns.length;i++){
					_newColumns.push(_settings.columns[i])
				}
				_settings.columns = _newColumns;
			}
		}else if(_settings.select.type=='checkbox'){
			var _checkbox = {
				"sTitle" : "<input type='checkbox' name='' value='' class='selectall'>",
				"sWidth" : "13",
				"render" : function(data, type, row) {
					return '<input type="checkbox" name="id[]" value="'
							+ row[_settings.select.name] + '">';
				}
			};
			if(_settings.columns){
				var _newColumns = [];
				_newColumns.push(_checkbox);
				for(var i=0;i<_settings.columns.length;i++){
					_newColumns.push(_settings.columns[i])
				}
				_settings.columns = _newColumns;
			}
		}
		
		/**
		 * 初始化表格
		 */
		var _inittable = function(){
			var _tableStr = "<table class='table table-border table-bordered table-bg table-hover table-sort' ";
			if(_settings.width='auto'){
				_tableStr +=" width='100%'";
			}else{
				_tableStr +=" width='"+_settings.width+"'";
			}
			_tableStr +="></table>";
			$(_tablethis).html(_tableStr);
			return $(_tablethis).find(".table-sort").dataTable(_settings);
		}
		var _dataTable = _inittable();
		
		/**
		 * 单击事件
		 */
		$(_tablethis).find('.table-sort tbody').on('click', 'tr', function(){
			var tr = $(this).closest('tr');	
			//单击选中
			if(_settings.select.type!='default'){								
				if(tr.find("input[name='id[]']").prop('checked')){
					tr.find("input[name='id[]']").prop('checked',false);
				}else{
					tr.find("input[name='id[]']").prop('checked',true);
				}
			}
			_settings.click();	
		});
		
		/**
		 * 双击事件
		 */
		$(_tablethis).find('.table-sort tbody').on('dblclick', 'tr', function(){_settings.dBclick()});
		
		/**
		 * 全选和反选
		 */
		$(_tablethis).find('.selectall').click(function() {
			if ($(_tablethis).find('.selectall').prop('checked')) {
				$(_tablethis).find("input[name='id[]']").prop('checked',true);
			} else {
				$(_tablethis).find("input[name='id[]']").prop('checked',false);
			}
		});
		
		
		var returnfun = {				
			/**
			 * dataTable对象
			 */
			dataTable : _dataTable,
			/**
			 * 选中行数
			 */
			getSelectNum : function(){
				if(_settings.select.type == 'radio'){
					var value = $(_tablethis).find("input[name='id[]']:radio:checked").val();
					if(value){
						return 1;
					}else{
						return 0;
					}
				}else if(_settings.select.type == 'checkbox'){
					var i=0;
					$(_tablethis).find("input[name='id[]']:checkbox").each(function(){
						if(true == $(this).prop('checked')){
							i=i+1;
						}
					}); 
					return i;
				}else{
					return 0;
				}
			},
			/**
			 * 选中行值
			 */
			getSelects : function(){
				if(_settings.select.type == 'radio'){
					return $(_tablethis).find("input[name='id[]']:radio:checked").val();
				}else if(_settings.select.type == 'checkbox'){
					var ids;
					$(_tablethis).find("input[name='id[]']:checkbox").each(function(){
						if(true == $(this).prop('checked')){
							ids += $(this).val()+',';
						}
					}); 
					return ids;
				}else{
					return ;
				}
			},
			/**
			 * 刷新表格
			 */
			draw : function(){
				_dataTable.DataTable().draw();
			}
			
		}
		return returnfun;
	}	
}(jQuery));
